<%--
  Created by IntelliJ IDEA.
  User: cyrus
  Date: 2021/04/10
  Time: 15:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>修改</title>
    <%@include file="../../include/include.jsp" %>
    <style>
        .layui-form-checkbox span {
            height: auto;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <input type="hidden" id="roleId" name="roleId"
                   class="layui-input" value="${entity.roleId}">


            <div class="layui-form-item">
                <label for="roleName" class="layui-form-label">
                    <span class="x-red">*</span>角色名
                </label>

                <div class="layui-input-inline">
                    <input type="text" id="roleName" name="roleName" required="" autocomplete="off"
                           class="layui-input" disabled="disabled" value="${entity.roleName}">
                </div>

                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>

            </div>


            <div class="layui-form-item">
                <label for="roleCode" class="layui-form-label">
                    <span class="x-red">*</span>角色码</label>
                <div class="layui-input-inline">
                    <input type="text" id="roleCode" name="roleCode" required="" lay-verify="roleCode"
                           autocomplete="off" class="layui-input" value="${entity.roleCode}"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>资源选择</label>
                <div class="layui-input-inline">
                    <c:forEach items="${resourceList}" var="resource">
                        <c:if test="${selectedResourceIdList.contains(resource.resourceId)}">
                            <input type="checkbox" name="resourceList" title="${resource.resourceName}"
                                   value="${resource.resourceId}"
                                   lay-skin="primary" checked>
                        </c:if>
                        <c:if test="${!selectedResourceIdList.contains(resource.resourceId)}">
                            <input type="checkbox" name="resourceList" title="${resource.resourceName}"
                                   value="${resource.resourceId}"
                                   lay-skin="primary">
                        </c:if>
                    </c:forEach>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="status" class="layui-form-label">
                    <span class="x-red">*</span>状态</label>
                <div class="layui-input-inline">
                    <input type="text" id="status" name="status" required="" lay-verify="status"
                           autocomplete="off" class="layui-input" value="${entity.status}"></div>
            </div>

            <div class="layui-form-item">
                <button class="layui-btn" lay-filter="add" lay-submit="">修改</button>
            </div>
        </form>
    </div>
</div>
<script>

    layui.use(['form', 'layer', 'jquery', 'laydate'], function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#birthday',
            trigger: "click",
            done(value, date, endDate) {
                $("#birthday").val(value);
            }
        });


        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
        });

        //监听提交
        form.on('submit(add)',
            function (data) {
                console.log(data);
                //发异步，把数据提交给php

                var selectResourceElList = $("input[name='resourceList']:checked")
                var resourceList = [];

                // let const
                for (var item of selectResourceElList) {
                    resourceList.push({resourceId:item.value})
                }
                data.field.resourceList = resourceList;

                $.ajax({
                    type: "POST",
                    url: "${ctx}/role/edit",
                    data: JSON.stringify(data.field),
                    // // 设置请求头为json格式
                    contentType: "application/json;charset=utf-8",

                    success(res) {
                        console.log(res)
                        if (res.success) {
                            layer.alert("增加成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.alert(res.msg)
                        }

                    }
                });
                return false;
            });

    });</script>
<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>
</html>
